const express = require('express')
const multer = require('multer')
const router = express.Router()
const path = require('path')

const TEMP_DIR = path.join(__dirname, 'temp')

// 配置multer处理文件上传
const chunkUpload = multer({
  storage: multer.diskStorage({
    destination: (req, file, cb) => {
      const { uploadId } = req.query
      const chunkDir = path.join(TEMP_DIR, uploadId, 'chunks')
      cb(null, chunkDir)
    },
    filename: (req, file, cb) => {
      const { chunkIndex } = req.query
      cb(null, `chunk-${chunkIndex}`)
    }
  }),
  limits: {
    fileSize: 1024 * 1024 * 100 // 限制单个分片100MB
  }
})

router.post('/', chunkUpload.single('file'), (req, res) => {
  const { chunkIndex, uploadId } = req.query
  // console.log(req.query)

  res.send({
    code: 200,
    message: 'success',
    data: {
      chunkIndex,
      uploadId
    }
  })
})

module.exports = router
